package cn.tedu.recruitmentwebsite.employee.service.impl;

import cn.tedu.recruitmentwebsite.employee.pojo.dto.PageQueryTalentParam;
import cn.tedu.recruitmentwebsite.base.pojo.entity.*;
import cn.tedu.recruitmentwebsite.base.pojo.vo.TalentVO;
import cn.tedu.recruitmentwebsite.base.response.PageResult;
import cn.tedu.recruitmentwebsite.employee.mapper.EmployeeMapper;
import cn.tedu.recruitmentwebsite.employee.service.EmployeeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Slf4j
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public List<String> selectCompanies() {
        List<String> companyNames =  employeeMapper.selectCompanies();
        return companyNames;
    }

    @Override
    public PageResult getPageQueryTalent(PageQueryTalentParam pageQuery) {
        // 开始分页查询
        PageHelper.startPage(pageQuery.getPageNum(),pageQuery.getPageSize());
        Page<OnlineResumes> page =  employeeMapper.getPageQueryTalent(pageQuery);
        long total = page.getTotal();

        List<TalentVO> talents = new ArrayList<>();
        for (OnlineResumes onlineResume : page.getResult()) {
            TalentVO talentVO = new TalentVO();
            log.debug("牛马在线简历信息：{}",onlineResume);
            talentVO.setJobStatus(onlineResume.getJobStatus());
            talentVO.setSalary(onlineResume.getMinSalary()+"-"+onlineResume.getMaxSalary());
            talentVO.setWorkCity(onlineResume.getWorkCity());
            talentVO.setTargetPosition(onlineResume.getTargetPosition());

            //获取教育背景信息
            List<JobSeekerEducationExperience> educationExperiences
                    = employeeMapper.getEducationExperience(onlineResume.getId());
            talentVO.setEducationExperiences(educationExperiences);
            //获取工作经历信息
            List<JobSeekerWorkExperience> workExperiences
                    = employeeMapper.getWorkExperience(onlineResume.getId());
            talentVO.setWorkExperiences(workExperiences);
            //获取项目经历信息
            List<ProjectExperience> projectExperiences
                    = employeeMapper.getProjectExperience(onlineResume.getId());
            talentVO.setProjectExperiences(projectExperiences);
            //获取用户信息
            User user = employeeMapper.getUserByOnlineResumesId(onlineResume.getUserId());
            talentVO.setNickname(user.getNickname());
            LocalDate birth = user.getBirthday().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            talentVO.setAge(String.valueOf(LocalDate.now().getYear() - birth.getYear()));

            log.debug("牛马完整信息:{}",talentVO);
            talents.add(talentVO);
        }
        log.debug("查询到所有的牛马信息talents：{}",talents);
        return new PageResult(total,talents);
    }

    @Override
    public List<TalentVO> getRecommendTalent(long total) {
        List<TalentVO> lists = employeeMapper.getRecommendTalent();
        log.debug("推荐人才总数：{}",lists.size());
        List<TalentVO> talents = new ArrayList<>();
        if (lists.size() < total){
            talents.addAll(lists);
        }else {
            Random random = new Random();
            for (int i = 0; i < total; i++) {
                int index = random.nextInt(lists.size());
                talents.add(lists.get(index));
                lists.remove(index);
            }
        }
        for (TalentVO talent : talents) {
            //计算年龄
            LocalDate birthday = talent.getBirthday().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            talent.setAge(String.valueOf(LocalDate.now().getYear() - birthday.getYear()));
            //获取在线简历
            OnlineResumes onlineResume
                    = employeeMapper.getOnlineResumesById(talent.getId());
            if (onlineResume != null){
                BeanUtils.copyProperties(onlineResume,talent);
                talent.setSalary(onlineResume.getMinSalary()+"-"+onlineResume.getMaxSalary());
                log.debug("在线简历信息：{}",talent);
                //获取教育背景信息
                List<JobSeekerEducationExperience> educationExperiences
                        = employeeMapper.getEducationExperience(talent.getId());
                talent.setEducationExperiences(educationExperiences);
                log.debug("已添加教育背景信息：{}",talent);
                //获取工作经历信息
                List<JobSeekerWorkExperience> workExperiences
                        = employeeMapper.getWorkExperience(talent.getId());
                talent.setWorkExperiences(workExperiences);
                log.debug("已添加工作经历信息：{}",talent);
                //获取项目经历信息
                List<ProjectExperience> projectExperiences
                        = employeeMapper.getProjectExperience(talent.getId());
                talent.setProjectExperiences(projectExperiences);
                log.debug("已添加项目经历信息：{}",talent);
            }
        }
        log.debug("推荐人才信息：{}",talents);
        return talents;
    }

}
